package com.wh1200.mybatis.xmlless.model

import com.wh1200.mybatis.xmlless.model.base.NameAlias


/**
 * 数据库表名称
 * @author 吴昊
 * @since 0.0.3
 * @param name 表名称
 * @param alias 表别名
 */
class TableName(name: String, alias: String = "", val schema: String? = null) : NameAlias(name, alias) {

  companion object {
    fun resolveWithPrefix(
        targetTable: String,
        aliasPrefix: String? = null,
        isPrefix: Boolean = true
    ): TableName {
      val nameAlias = NameAlias.resolveWithPrefix(targetTable, aliasPrefix, isPrefix)
      return TableName(nameAlias.name, nameAlias.alias)
    }
  }

  fun getAliasOrName(): String {
    if (alias.isNotBlank()) {
      return alias
    }
    return name
  }

  override fun toString(): String {
    return if (alias.isNotBlank()) {
      "$name AS $alias"
    } else {
      name
    }
  }

}
